package com.example.travelgd.controller;

import com.example.travelgd.entity.Specialty;
import com.example.travelgd.service.SpecialtyService;
import com.example.travelgd.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/specialty")
@Tag(name = "SpecialtyController", description = "特产推荐管理")
public class SpecialtyController {

    @Autowired
    private SpecialtyService specialtyService;

    @GetMapping("/list")
    @Operation(summary = "获取特产列表")
    public FwResult<List<Specialty>> getSpecialtyList(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        try {
            List<Specialty> specialtyList = specialtyService.getSpecialtyList(categoryId, page, pageSize);
            log.info("获取特产列表成功, 分类: {}, 数据: {}", categoryId, specialtyList);
            return FwResult.ok(specialtyList);
        } catch (Exception e) {
            log.error("获取特产列表失败", e);
            return FwResult.failedMsg("获取特产列表失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取特产详情")
    public FwResult<Specialty> getSpecialtyDetail(@PathVariable Long id) {
        try {
            Specialty specialty = specialtyService.getSpecialtyDetail(id);
            log.info("获取特产详情成功, id: {}, 数据: {}", id, specialty);
            return FwResult.ok(specialty);
        } catch (Exception e) {
            log.error("获取特产详情失败, id: {}", id, e);
            return FwResult.failedMsg("获取特产详情失败");
        }
    }

    @PostMapping
    @Operation(summary = "添加特产")
    public FwResult<Void> addSpecialty(@RequestBody Specialty specialty) {
        return specialtyService.addSpecialty(specialty);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新特产")
    public FwResult<Void> updateSpecialty(@PathVariable Long id, @RequestBody Specialty specialty) {
        specialty.setId(id);
        return specialtyService.updateSpecialty(specialty);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除特产")
    public FwResult<Void> deleteSpecialty(@PathVariable Long id) {
        return specialtyService.deleteSpecialty(id);
    }
}